<?php

function addSMS2($config,$pesan,$dari,$ke){
	$ukey = $config['ukey'];
	$pkey = $config['pkey'];
	$phones = addPhone($config,$ke);
	$message = "[SMS Idebeda]\nDari : $dari\n\"$pesan\"";
	$message = helper::trimMsg($message,140);
	//var_dump($phones);
	foreach ($phones as $nomor) {
		$url = 'http://zenziva.com/apps/smsapi.php?userkey='.$ukey.'&passkey='.$pkey.'&nohp='.$nomor.'&pesan='.urlencode($message);
		$curlHandle = curl_init();
		curl_setopt($curlHandle, CURLOPT_URL, $url);
		//curl_setopt($curlHandle, CURLOPT_HEADER, 0);
		curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curlHandle, CURLOPT_USERAGENT, 'Codular Sample cURL Request');
		curl_setopt($curlHandle, CURLOPT_TIMEOUT,30);
		$results = curl_exec($curlHandle);
		curl_close($curlHandle);
	}
}

function controller_sms($config) {
	if (isset($_SESSION['logged'])){
		if (isset($_POST['sms'])){
			//masukkkan
			//$date = date('Y-m-d H:i:s', time());
			$dari = $_SESSION['logged']['id'];
			$sender = $_SESSION['logged']['uname'];
			$tujuans = $_POST['ckbox'];
			$pesan = $_POST['pesan'];
			foreach ($tujuans as $tujuan){
				//echo $tujuan;
				addSMS2($config,$pesan,$sender,$tujuan);
				if ($tujuan=='0') break;
			}
			//SMS
			$_SESSION['addsucc'] = "Pesan berhasil dikirim";
			header('Location: '.$config['home_url']."sms");
		}else{
			//utama
			view_sms($config,array());
		}
	}else
		header('Location: '.$config['home_url']);
}
